package com.iotproject.forumservice.service.impl;

import com.iotproject.commonutils.forumvo.UcenterMemberForum;
import com.iotproject.forumservice.client.UcenterClient;
import com.iotproject.forumservice.entity.ForumNotification;
import com.iotproject.forumservice.entity.vo.AddNotificationVo;
import com.iotproject.forumservice.mapper.ForumNotificationMapper;
import com.iotproject.forumservice.service.ForumNotificationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息通知 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-15
 */
@Service
public class ForumNotificationServiceImpl extends ServiceImpl<ForumNotificationMapper, ForumNotification> implements ForumNotificationService {

    @Autowired
    UcenterClient ucenterClient;

    @Override
    public Map<String, Object> getNotificationList(String userId, Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<ForumNotification> notificationList = baseMapper.getNotificationList(userId, page, size);
        QueryWrapper<ForumNotification> forumNotificationQueryWrapper = new QueryWrapper<>();
        forumNotificationQueryWrapper.eq("receiver", userId);
        Integer total = baseMapper.selectCount(forumNotificationQueryWrapper);
        Map<String, Object> maps = new HashMap<>();
        maps.put("list", notificationList);
        maps.put("total", total);
        return maps;
    }

    @Override
    public void updateStatus(String notificationId) {
        baseMapper.updateStatus(notificationId);
    }

    @Override
    public int addNotification(AddNotificationVo addNotificationVo) {
        ForumNotification forumNotification = new ForumNotification();
        BeanUtils.copyProperties(addNotificationVo, forumNotification);
        forumNotification.setStatus(false);
        UcenterMemberForum notifierInfo = ucenterClient.getUserInfoForum(forumNotification.getNotifier());
        forumNotification.setNotifierName(notifierInfo.getNickname());
        int insert = baseMapper.insert(forumNotification);
        return insert;
    }
}
